<?php
require 'BaseAction.class.php';
import('ORG.Util.Input');
import('@.ORG.FAC');
/**
 * 管理员
 * Enter description here ...
 * @author Code.404
 *
 */
class AdminUserAction extends BaseAction{
	
	function _init(){
		$super_list = array('deleteUser','editUser');
		if(in_array(ACTION_NAME, $super_list)){
			if($_SESSION['super_admin'] != 'super')
				exit('Forbidden!');
		}
		//需要日志记录
		
		$log_list = array('addUser', 'deleteUser' ,'editUser' , 'changePassword');
		if(C('LOG_ADMIN') && in_array(ACTION_NAME, $log_list))
			 D('Log')->writeLog();
	}
	
	/**
	 * 添加新用户
	 * Enter description here ...
	 */
	function addUser(){
		if($this->_a == 'show' && $this->_t == 'dialog'){
			$this->display('AdminUser:_addUser');
		}
		if($this->_a == 'exec'){
			$userModel = D('Admin');
			try{
				if ($userModel->addUser($_POST)){
					$this->ajaxJsonReturn(200 , L('ACTION_SUCCESS'), 0 , '' ,'closeCurrent');
				}
				$this->ajaxJsonReturn(300 ,L( $userModel->getError()));
			}catch(Exception $e){
				$this->ajaxJsonReturn(300,L($e->getMessage()));
			}
		}
	}
	/**
	 * 删除用户
	 * Enter description here ...
	 */
	function deleteUser(){
		try{
			$id = FAC::F($_GET['id'],FAC::INT);
			if($id == $_SESSION['admin_user']['id']){
				$this->ajaxJsonReturn(300 ,L('CAN_NOT_DEL_SELF'));
			}
			if(D('Admin')->delete($id)){
				$this->ajaxJsonReturn(200 , L('ACTION_SUCCESS'),0,U('AdminUser/managerList'),'forward');
			}
			$this->ajaxJsonReturn(300 ,L('ACTION_FAILED'));
		}catch (Exception $e){
			$this->ajaxJsonReturn(300,L($e->getMessage()));
		}
	}
	/**
	 * 编辑用户信息
	 * Enter description here ...
	 */
	function editUser(){
		if($this->_a == 'show' && $this->_t == 'dialog'){
			if($_GET['id'] != $_SESSION['admin_user']['id']){
				$this->assign('notNeedAuth' , true);
			}
			
			$this->assign('user',D('Admin')->find(FAC::F($_GET['id'],FAC::INT)));
			$this->display('AdminUser:_changePassword');
		}
		if($this->_a == 'exec'){
			try{
				$_map['id'] = FAC::F($_POST['uid'],FAC::INT);
				$pwd = FAC::F($_POST['password'],FAC::PASSWORD);
				
				if($_map['id'] == $_SESSION['admin_user']['id']){
					$this->ajaxJsonReturn(300 ,L('ACTION_FAILED'));
				}
				
				$userModel = D('Admin');
				if($userModel->where($_map)->setField('password',$pwd)){
					$this->ajaxJsonReturn(200 , L('ACTION_SUCCESS'), 0 , '' ,'closeCurrent');
				}
				$this->ajaxJsonReturn(300 ,L('ACTION_FAILED'));
			}catch (Exception $e){
				$this->ajaxJsonReturn(300,L($e->getMessage()));
			}
		}
	}
	/**
	 * 修改密码
	 * 当前登陆管理员修改个人密码
	 */
	function changePassword(){
		if($this->_a == 'show' && $this->_t == 'dialog'){
			$this->display('AdminUser:_changePassword');
		}
		if($this->_a == 'exec'){
			$userModel = D('Admin');
			try{
				$_map['id'] = $_SESSION['admin_user']['id'];
				$_map['password'] = FAC::F($_REQUEST['old_password'],FAC::PASSWORD);

				if($userModel->where($_map)->find()){
					$userModel->password = FAC::F($_REQUEST['password'],FAC::PASSWORD);
					$userModel->save() ? 
						$this->ajaxJsonReturn(200 , L('ACTION_SUCCESS'), 0 , '' ,'closeCurrent') :
						$this->ajaxJsonReturn(300,L('ACTION_FAILED'));
				}else{
					$this->ajaxJsonReturn(300,L('OLD_PASSWORD_WRONG'));
				}
			}catch (Exception $e){
				$this->ajaxJsonReturn(300,L($e->getMessage()));
			}
		}
	}
	/**
	 * 管理员个人信息
	 * 当前已经登陆管理员个人信息
	 */
	function personalInfo(){
		$this->assign('admin' , $_SESSION['admin_user']);
		$this->display('AdminUser:_personalInfo');
	}
	/**
	 * 管理员列表
	 * 管理员查看当前管理员列表
	 */
	function managerList(){
		$this->assign('adminList' , D('Admin')->order('id desc')->select());
		$this->display();
	}
}